﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using WPFContrib;
using FasterWPF;
using WPFContribDataAccess;
using System.Windows.Media;
using System.Windows;

namespace WPFContribPresenters
{
    public class PageSamplePresenter
    {
            private SimplePagingControl pgs = null;

            public SimplePagingControl PagingControl
            { 
                get { return pgs; }  
            }

            public PageSamplePresenter()
            {
              pgs = new SimplePagingControl(23.0, 0.0, "Paging Control - 250 Records Per Page (Page Recycled)");
            }

            public int GetTotalNumberOfPages(long totalRecords)
            {
              return pgs.DivideTotalRecordsAlwaysRoundingUp(totalRecords, 250);
            }

            public int GetStartNumberFromTextBlock()
            {
                return pgs.GetStartNumber();
            }
            
            public int GetNumberOfRecordsInMainDataGrid()
            {
                return pgs.MainDataGrid.Items.Count;
            }

            public Action ActionPageNext(StackPanel pagingControlStackPanel)
            {
                Action retVal = ()=>{
                var firePageNext = pgs.NextButtonSingleClick;
                firePageNext(pgs, pagingControlStackPanel);
                };

                return retVal;
            }

            public Action ActionPagePrev(StackPanel pagingControlStackPanel)
            {
                Action retVal = ()=>{
                var firePagePrevious = pgs.PreviousButtonSingleClick;
                firePagePrevious(pgs, pagingControlStackPanel);
                };

                return retVal;
            }

            public void FirePageNext(StackPanel pagingControlStackPanel)
            {
                var firePageNext = pgs.NextButtonSingleClick;
                firePageNext(pgs, pagingControlStackPanel);
            }

            public void FirePagePrevious(StackPanel pagingControlStackPanel)
            {
                var firePagePrevious = pgs.PreviousButtonSingleClick;
                firePagePrevious(pgs, pagingControlStackPanel);
            }

            public StackPanel BuildPagingControl(long totalRecords, ISimpleDataAccess sqliteDataAccess)
            {
               //WITH PAGE RECYCLING - FAST!!!
               StackPanel mainPanel = pgs.BuildPagingControl(557.0D + 23.0, 30.0, pgs.DivideTotalRecordsAlwaysRoundingUp(totalRecords, 250), 250,
                                  (mainDataGrid, fromNum, toNum) =>
                                  {
                                      SimplePagingControl pgsCopy = pgs;
                                      DataGrid myDataGridCopy1 = mainDataGrid;

                                      ISimpleDataAccess sqliteDataAccessCopy = sqliteDataAccess;
                                      int counter = 0;

                                      sqliteDataAccessCopy.LoadData("SECDATA", fromNum.ToString(), (toNum - fromNum).ToString(), dr =>
                                      {
                                          DataGrid myDataGridCopy2 = mainDataGrid;

                                          string x = Guid.NewGuid().ToString();
                                          string name = dr[4].ToString();
                                          if (name.Length > 50)
                                          {
                                              name = name.Substring(0, 50) + "...";
                                          }

                                          ////Column 1
                                          string compositeKey = (myDataGridCopy2.GetCompositeLookupDictionary().Values.ElementAt(counter) as Border).GetKey();
                                          FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey, 0, 0, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[0].ToString()));
                                          FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey, 0, 1, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[1].ToString()));
                                          FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey, 0, 2, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, name));

                                          counter++;
                                          ////Column 2
                                          string compositeKey2 = (myDataGridCopy2.GetCompositeLookupDictionary().Values.ElementAt(counter) as Border).GetKey();
                                          FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey2, 0, 0, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[8].ToString()));
                                          FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey2, 0, 1, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[9].ToString()));
                                          FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey2, 0, 2, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[10].ToString() + " " + dr[11].ToString() + " " + dr[12].ToString()));

                                          counter++;
                                      });

                                      //if existing containers not used up, then blank out any remaining, un-recycled containers
                                      if (counter != ((toNum - fromNum) * 2))
                                      {
                                          for (int n = counter; n < ((toNum - fromNum) * 2); n++)
                                          {
                                              int countOfComposites = mainDataGrid.GetCompositeLookupDictionary().Values.Count;

                                              if (n < countOfComposites)
                                              {
                                                  ////Column 1
                                                  string compositeKey = (mainDataGrid.GetCompositeLookupDictionary().Values.ElementAt(n) as Border).GetKey();
                                                  FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(mainDataGrid, compositeKey, 0, 0, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, String.Empty));
                                                  FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(mainDataGrid, compositeKey, 0, 1, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, String.Empty));
                                                  FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(mainDataGrid, compositeKey, 0, 2, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, String.Empty));
                                              }

                                              n++;

                                              if (n < countOfComposites)
                                              {
                                                  ////Column 2
                                                  string compositeKey2 = (mainDataGrid.GetCompositeLookupDictionary().Values.ElementAt(n) as Border).GetKey();
                                                  FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(mainDataGrid, compositeKey2, 0, 0, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, String.Empty));
                                                  FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(mainDataGrid, compositeKey2, 0, 1, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, String.Empty));
                                                  FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(mainDataGrid, compositeKey2, 0, 2, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, String.Empty));
                                              }
                                          }
                                      }
                                  },

               (mainDataGrid, fromNum, toNum) =>
               {
                   SimplePagingControl pgsCopy = pgs;
                   DataGrid myDataGridCopy1 = mainDataGrid;

                   ISimpleDataAccess sqliteDataAccessCopy = sqliteDataAccess;
                   int counter = 0;
                   sqliteDataAccessCopy.LoadData("SECDATA", fromNum.ToString(), (toNum - fromNum).ToString(), dr =>
                   {
                       DataGrid myDataGridCopy2 = mainDataGrid;

                       string x = Guid.NewGuid().ToString();
                       string name = dr[4].ToString();
                       if (name.Length > 50)
                       {
                           name = name.Substring(0, 50) + "...";
                       }

                       ////Column 1
                       string compositeKey = (myDataGridCopy2.GetCompositeLookupDictionary().Values.ElementAt(counter) as Border).GetKey();
                       FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey, 0, 0, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[0].ToString()));
                       FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey, 0, 1, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[1].ToString()));
                       FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey, 0, 2, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, name));

                       counter++;
                       ////Column 2
                       string compositeKey2 = (myDataGridCopy2.GetCompositeLookupDictionary().Values.ElementAt(counter) as Border).GetKey();
                       FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey2, 0, 0, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[8].ToString()));
                       FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey2, 0, 1, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[9].ToString()));
                       FasterWPF.CommonExt.UpdateByKey<TextBlock, DataGrid>(myDataGridCopy2, compositeKey2, 0, 2, txtBlck => FasterWPF.TextBlockExt.SetText(txtBlck, dr[10].ToString() + " " + dr[11].ToString() + " " + dr[12].ToString()));

                       counter++;
                   });
               });

               List<string> columnHeaderList = new List<string>();
               columnHeaderList.Add(" Name                                ");
               columnHeaderList.Add(" Address                             ");

               StackPanel pagingControlStackPanel = pgs.BuildDataGridInStackPanel(557.0D, 703.0D, columnHeaderList, mainPanel);

               return pagingControlStackPanel;
            }

            public void PerformInitialLoadOfData(StackPanel pagingControlStackPanel, ISimpleDataAccess sqliteDataAccess)
            {
                pgs.TitleLabel.Margin = new Thickness(23.0D, 0.0D, 0.0D, 15.0D);
            
                //Load first 350
                int counter2 = 0;
                sqliteDataAccess.LoadData("SECDATA", 0.ToString(), 250.ToString(), dr =>
                {
                    DataGrid myDataGridCopy2 = pgs.MainDataGrid;
                      
                     string x = Guid.NewGuid().ToString();
                     string name = dr[4].ToString();
                     if(name.Length>50)
                     {
                        name=name.Substring(0,50)+"...";
                     }

                     //Column 1
                     myDataGridCopy2.BeginComposite(x + "1")
                                                 .AddText(0, 0, dr[0].ToString())
                                                 .AddText(0, 1, dr[1].ToString())
                                                 .AddText(0, 2, name)
                                                 .SetMouseOverColorOnContainer<StackPanel, DataGrid>(Brushes.Green)
                                                 .EndComposite<DataGrid, DataGridArgs>(new DataGridArgs(x, counter2, 0, pgs.ColumnHeaderList));
                     //Column 2
                     myDataGridCopy2.BeginComposite(x + "2")
                                           .AddText(0, 0, dr[8].ToString())
                                           .AddText(0, 1, dr[9].ToString())
                                           .AddText(0, 2, dr[10].ToString()+" "+dr[11].ToString()+" "+dr[12].ToString())
                                           .SetMouseOverColorOnContainer<StackPanel, DataGrid>(Brushes.Green)
                                           .EndComposite<DataGrid, DataGridArgs>(new DataGridArgs(x, counter2, 1, pgs.ColumnHeaderList));
                     counter2++;
               }
               );              
            }                  
    }
}



//EXAMPLE WITHOUT PAGE RECYCLING - SLOW!!! 
//
//(YET, THIS MAY BE NECESSARY ON ADDING/UPDATING RECORDS OR INITIAL SEARCHING/FILTERING?)

//StackPanel mainPanel = pgs.BuildPagingControl(557.0D + 23.0, 30.0, pgs.DivideTotalRecordsAlwaysRoundingUp(totalRecords, 100), 100,

//    (mainDataGrid, fromNum, toNum) =>
//    {
//        SimplePagingControl pgsCopy = pgs;
//        DataGrid myDataGridCopy1 = mainDataGrid;

//        if (myDataGridCopy1.ItemsSource != null && myDataGridCopy1.Items.Count != 0)
//        {
//            FasterWPF.Selectors.DisposeEventsOnAllParents(myDataGridCopy1);
//            FasterWPF.CommonExt.RemoveAll(myDataGridCopy1);
//            System.Windows.Data.CollectionViewSource.GetDefaultView(myDataGridCopy1.ItemsSource).Refresh();
//        }
//        else
//        {
//            FasterWPF.CommonExt.RemoveAll(myDataGridCopy1);
//        }

//        SQLParms sqlParmsCopy = sql_parms;
//        int counter = 0;
//        DataAccess.LoadData(sqlParmsCopy, fromNum.ToString(), (toNum - fromNum).ToString(), dr =>
//        {
//            DataGrid myDataGridCopy2 = mainDataGrid;

//            string x = Guid.NewGuid().ToString();
//            string name = dr[4].ToString();
//            if (name.Length > 50)
//            {
//                name = name.Substring(0, 50) + "...";
//            }

//            //Column 1
//            myDataGridCopy2.BeginComposite(x + "1")
//                                 .AddText(0, 0, dr[0].ToString())
//                                 .AddText(0, 1, dr[1].ToString())
//                                 .AddText(0, 2, name)
//                                 .SetMouseOverColorOnContainer<StackPanel, DataGrid>(Brushes.Green)
//                                 .EndComposite<DataGrid, DataGridArgs>(new DataGridArgs(x, counter, 0, pgs.ColumnHeaderList));
//            //Column 2
//            myDataGridCopy2.BeginComposite(x + "2")
//                           .AddText(0, 0, dr[8].ToString())
//                           .AddText(0, 1, dr[9].ToString())
//                           .AddText(0, 2, dr[10].ToString() + " " + dr[11].ToString() + " " + dr[12].ToString())
//                           .SetMouseOverColorOnContainer<StackPanel, DataGrid>(Brushes.Green)
//                           .EndComposite<DataGrid, DataGridArgs>(new DataGridArgs(x, counter, 1, pgs.ColumnHeaderList));

//            counter++;
//        });

//    },

//    (mainDataGrid, fromNum, toNum) =>
//    {

//        SimplePagingControl pgsCopy = pgs;
//        DataGrid myDataGridCopy1 = mainDataGrid;

//        if (myDataGridCopy1.ItemsSource != null && myDataGridCopy1.Items.Count!=0)
//        {
//            FasterWPF.Selectors.DisposeEventsOnAllParents(myDataGridCopy1);
//            FasterWPF.CommonExt.RemoveAll(myDataGridCopy1);
//            System.Windows.Data.CollectionViewSource.GetDefaultView(myDataGridCopy1.ItemsSource).Refresh();
//        }
//        else
//        {
//            FasterWPF.CommonExt.RemoveAll(myDataGridCopy1);
//        }

//        SQLParms sqlParmsCopy = sql_parms;
//        int counter = 0;
//        DataAccess.LoadData(sqlParmsCopy, fromNum.ToString(), (toNum - fromNum).ToString(), dr =>
//        {
//            DataGrid myDataGridCopy2 = mainDataGrid;

//            string x = Guid.NewGuid().ToString();
//            string name = dr[4].ToString();
//            if (name.Length > 50)
//            {
//                name = name.Substring(0, 50) + "...";
//            }

//            //Column 1
//            myDataGridCopy2.BeginComposite(x + "1")
//                                 .AddText(0, 0, dr[0].ToString())
//                                 .AddText(0, 1, dr[1].ToString())
//                                 .AddText(0, 2, name)
//                                 .SetMouseOverColorOnContainer<StackPanel, DataGrid>(Brushes.Green)
//                                 .EndComposite<DataGrid, DataGridArgs>(new DataGridArgs(x, counter, 0, pgs.ColumnHeaderList));
//            //Column 2
//            myDataGridCopy2.BeginComposite(x + "2")
//                           .AddText(0, 0, dr[8].ToString())
//                           .AddText(0, 1, dr[9].ToString())
//                           .AddText(0, 2, dr[10].ToString() + " " + dr[11].ToString() + " " + dr[12].ToString())
//                           .SetMouseOverColorOnContainer<StackPanel, DataGrid>(Brushes.Green)
//                           .EndComposite<DataGrid, DataGridArgs>(new DataGridArgs(x, counter, 1, pgs.ColumnHeaderList));
//            counter++;
//        });
//    });

